import java.util.Random;
import java.util.Scanner;

public class Lab_01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        int correctCount = 0;

        System.out.println("欢迎来到口算练习系统！");
        System.out.println("你将完成50道100以内的加减法题目");
        System.out.println("请认真作答，祝你好成绩！");
        System.out.println("========================");

        for (int i = 1; i <= 50; i++) {
            // 生成两个0-100之间的随机数
            int num1 = random.nextInt(101);
            int num2 = random.nextInt(101);

            // 随机决定是加法还是减法（0表示加法，1表示减法）
            int operation = random.nextInt(2);

            // 确保减法不会出现负数结果
            if (operation == 1 && num1 < num2) {
                int temp = num1;
                num1 = num2;
                num2 = temp;
            }

            String operator = (operation == 0) ? "+" : "-";
            int correctAnswer = (operation == 0) ? num1 + num2 : num1 - num2;

            System.out.printf("第%2d题: %d %s %d = ", i, num1, operator, num2);

            // 获取用户答案
            int userAnswer = scanner.nextInt();

            // 判断答案是否正确
            if (userAnswer == correctAnswer) {
                System.out.println("    ✓ 正确！");
                correctCount++;
            } else {
                System.out.printf("    ✗ 错误！正确答案是：%d\n", correctAnswer);
            }
        }

        System.out.println("========================");
        System.out.printf("练习结束！你总共答对了%d题，正确率为%.2f%%\n",
                correctCount, (correctCount / 50.0) * 100);

        scanner.close();
    }
}
